package com.hundun.hddeal.util;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;

import java.util.HashMap;
import java.util.Map;

public class NoteUtil {


    /**
     * 短信验证
     * @param phoneNumber
     * @return
     * @throws ClientException
     */
    public static String sendNote(String phoneNumber) throws ClientException {
        String randomNum = createRandomNum(6);
        System.out.println(randomNum);
        String jsonContent = "{\"code\":\"" + randomNum + "\"}";
        String templateCode = "SMS_162733477";
        util(phoneNumber,jsonContent,templateCode);
        return randomNum;
    }


    /**
     * 订单通知
     * @param phoneNumber
     * @throws ClientException
     */
    public static String getLoginNote(String phoneNumber) throws ClientException {
        //String jsonContent = "{\"product\":\"" + "" + "\"}";
        String randomNum = createRandomNum(6);
        String templateCode = "SMS_171356375";
        String jsonContent = "{\"code\":\"" + randomNum + "\"}";
        util(phoneNumber,jsonContent,templateCode);
        return randomNum;
    }



    /**
     * 订单通知
     * @param phoneNumber
     * @throws ClientException
     */
    public static void getNote(String phoneNumber) throws ClientException {
        //String jsonContent = "{\"product\":\"" + "" + "\"}";
        String templateCode = "SMS_162734038";
        util(phoneNumber,null,templateCode);
    }


    /**
     * 发送短信
     * @param phoneNumber
     * @param jsonContent
     * @param templateCode
     * @throws ClientException
     */
    public static void util(String phoneNumber,String jsonContent,String templateCode) throws ClientException {
        Map<String, String> paramMap = new HashMap<>(16);
        paramMap.put("phoneNumber", phoneNumber);
        paramMap.put("msgSign", "混沌网络");
        paramMap.put("templateCode", templateCode);
        if(jsonContent!=null){
            paramMap.put("jsonContent", jsonContent);
        }
        SendSmsResponse sendSmsResponse = AliyunMessageUtil.sendSms(paramMap);
        System.out.println(sendSmsResponse.getCode());
        if(!(sendSmsResponse.getCode() != null && "OK".equals(sendSmsResponse.getCode()))) {
            if(sendSmsResponse.getCode() == null) {
                //这里可以抛出自定义异常
            }
            if(!"OK".equals(sendSmsResponse.getCode())) {
                //这里可以抛出自定义异常
            }
        }
    }


    /**
     * 订单过期
     * @param phoneNumber
     * @throws ClientException
     */
    public static void sendMessage(String phoneNumber) throws ClientException {
        //String jsonContent = "{\"product\":\"" + "" + "\"}";
        String templateCode = "SMS_163438265";
        util(phoneNumber,null,templateCode);
    }












    /**
     * 生成随机数
     * @param num 位数
     * @return
     */
    public static String createRandomNum(int num){
        String randomNumStr = "";
        for(int i = 0; i < num;i ++){
            int randomNum = (int)(Math.random() * 10);
            randomNumStr += randomNum;
        }
        return randomNumStr;
    }











}
